<?php
/**
 * Created by PhpStorm.
 * User: qiaoli
 * Date: 15/9/7
 * Time: 下午4:28
 */
define('APIAUTHKEY','2d0424f34490260fe7799f81ad72fc96');
define('expiretime',60*60*24*90);
class Authenticatem extends CI_Model
{
    const  MC_LEGAL_INFO_KEY = 'is_legal_';
    function __construct()
    {
        $this->load->library('xxtea');
    }

    public function legal($token,$user_id=0, $isCookie = 0)
    {
        $mc = $this->cache->memcached;
        $key = self::MC_LEGAL_INFO_KEY . $this->pid.'_'.$this->uid;

        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }
        //如果是cookie 先去除
        $mc_token = $mc->get($key);
        if($mc_token && $mc_token == $token)
        {
            $result = array("code" => 0,'tips' => "鉴权成功");
            return $result;
        }
        else
        {
            //从数据库取
            $this->load->model('userm');
            $userinfo = $this->userm->getUserInfoById($this->uid,1);
            $password = $userinfo['password'];

            $token_pt = $this->parse_token($token);
            $token_info = explode('_',$token_pt);
            if(count($token_info) == 4)
            {
                if(($token_info[0] == $this->uid && $token_info[1] == $password && $token_info[2] == $this->pid) || ($isCookie == 1&& $token_info[1] == $password))
                {
                    $addtime = $token_info[3];
                    $gap = time() - $addtime;
                    if($gap < expiretime)
                    {
                        $mc->save($key, $token, $gap);
                        $result = array("code" => 0,'tips' => "鉴权成功");
                        return $result;
                    }
                    else
                    {
                        $result = array("code" => 101,'tips' => "token过期");
                        return $result;
                    }
                }
            }
        }
        $result = array("code" => 102,'tips' => "鉴权失败");
        return $result;
    }

    public function create_token($uid)
    {
        //从数据库取
        $this->load->model('userm');
        $userinfo = $this->userm->getUserInfoById($uid,1);
        $password = $userinfo['password'];

        $add_time = time();

        $token_str = $uid.'_'.$password.'_'.$this->pid.'_'.$add_time;

        $token = $this->xxtea->encrypt($token_str,APIAUTHKEY);

        $mc = $this->cache->memcached;
        $key = self::MC_LEGAL_INFO_KEY .$this->pid.'_'. $uid;

        if(!$this->cache->memcached->is_supported())
        {
            $result = array("code" => -3,'tips' => "缓存未开启");
        }

        $mc->save($key, $token, expiretime);
        return $token;
    }
    private function parse_token($token)
    {
        return $this->xxtea->decrypt($token,APIAUTHKEY);
    }

    /**
     * 令用户 token 过期
     * @param $uid
     */
    public function expire_token($uid)
    {
        $pid_list = ['2002', '3001', '4001', 'web', ''];
        $mc = $this->cache->memcached;
        $this->cache->memcached->is_supported();

        foreach ($pid_list as $pid) {
            $key = self::MC_LEGAL_INFO_KEY . $pid . '_' . $uid;
            $mc->delete($key);
        }
    }
}
